﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace Fabrikam.Phone.Converters
{
    public class BooleanToVisibilityConverter : IValueConverter
    {

        private bool inverted;

        public bool Inverted
        {
            get { return inverted; }
            set { inverted = value; }
        }


        private bool not;

        public bool Not
        {
            get { return not; }
            set { not = value; }
        }


        private object VisibilityToBool(object value)
        {

            if (!(value is Visibility))
                return DependencyProperty.UnsetValue;


            return (((Visibility)value) == Visibility.Visible) ^ Not;
        }



        private object BoolToVisibility(object value)
        {

            if (!(value is bool))
                return DependencyProperty.UnsetValue;

            if (Not)
                return (bool)value ? Visibility.Collapsed : Visibility.Visible;
            
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }



        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Inverted ? BoolToVisibility(value) : VisibilityToBool(value);
        }



        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Inverted ? VisibilityToBool(value) : BoolToVisibility(value);
        }

    }
}
